<{include file="public/header.html"}>
		<div class="xinsu-right">
			<div class="xinsu-right-title" id="xinsu-zhishu"></div>
			<div class="xinsu-right-content">
				<div class="xinsu-data-item2">
					<div class="xinsu-right-action">
						<div class="xinsu-right-action-top">
							<div class="xinsu-action">项目功能区</div>
							<button class="layui-btn xinsu-btn xinsu-refresh">刷新</button>
						</div>
						<ul class="xinsu-right-info">
							<li></li>
							<li></li>
							<li></li>
							<li></li>
							<li></li>
							<li></li>
							<div class="xinsu-clear"></div>
						</ul>
					</div>
				</div>	
				<div class="layui-form">
					<table class="layui-table">
						<thead>
							<tr>
								<th>编号</th>
								<th>证券代码</th>
								<th>证券名称</th>
								<th>类型</th>
								<th>证券数量</th>
								<th>可用数量</th>
								<th>成交价</th>
								<th>当前手续费</th>
								<th>最新价</th>
								<th>成交金额</th>
								<th>浮动盈亏</th>
								<th>结算费/天</th>
								<th>盈亏比例（%）</th>
								<th>建仓时间</th>
								<th>操作</th>
							</tr> 
						</thead>
						<tbody>
							<{if empty($list) == false}>
							<{foreach $list as $val}>
							<tr id="xinsu-stock-<{$val.id}>" class="xinsu-stock-status" data="<{$val.id}>">
								<{if $val.bull_trust_id > 0}>
								<td class="xinsu-fontcolor-red">委托<{$val.id}></td>
								<{else}>
								<td class="xinsu-fontcolor-blue">市价<{$val.id}></td>
								<{/if}>
								<td><{$val.stock_code}></td>
								<td><a class="xinsu-fontcolor-blue xinsu-stock-figure" href="javascript:;" data="<{$val.stock}>"><{if isset($close[$val["stock"]])}><{$close[$val["stock"]]}><{else}><{$val.stock_name}><{/if}></a></td>
								<{if $val.bull_type == 1}>
								<td class="xinsu-fontcolor-red" id="xinsu-stock-bulltype-<{$val.id}>">融资↑</td>
								<{else}>
								<td class="xinsu-fontcolor-green" id="xinsu-stock-bulltype-<{$val.id}>">融券↓</td>
								<{/if}>
								<td class="xinsu-fontcolor-blue"><{$val.bull_num}></td>
								<td class="xinsu-fontcolor-green"><{$val.bull_num - $val.sell_num}></td>
								<td class="xinsu-fontcolor-red"><{$val.bull_price}></td>
								<td class="xinsu-fontcolor-red"><{$val.bull_cost_money}></td>	
								<td id="xinsu-stock-price-<{$val.id}>">--</td>
								<td><{$val.bull_money}></td>	
								<td id="xinsu-stock-gain-<{$val.id}>">--</td>
								<td id="xinsu-stock-save-<{$val.id}>">--</td>
								<td id="xinsu-stock-loss-<{$val.id}>">--</td>
								<td><{date("Y-m-d H:i:s",$val["bull_time"])}></td>
								<td id="xinsu-stock-manage-<{$val.id}>">
									<a class="xinsu-fontcolor-blue xinsu-amend xinsu-block-sell" href="javascript:;" data="<{$val.id}>" data-num="共<{$val.bull_num}>（股） 已委托 <{$val.bull_num - $val.can_sell_num - $val.sell_num}>（股） 可卖：<{$val.can_sell_num}>（股）" data-bulltype="<{$val.bull_type}>" data-stock='<{$val.stock}>' data-cannum="<{$val.can_sell_num}>" data-stockname='<{$val.stock_name}>' data-bullprice="<{$val.bull_price}>" data-bulltype="<{$val.bull_type}>" data-bulltime="<{date('Y-m-d H:i:s',$val['bull_time'])}>"><{if $val.bull_type == 1}><button class="xinsu-fontcolor-green">卖券还款<button><{else}><button class="xinsu-fontcolor-red">买券还券<button><{/if}></a>
								</td>
							</tr>
							<{/foreach}>
							<{else}>
							<tr>
								<td colspan="15">暂无数据</td>
							</tr>
							<{/if}>
						</tbody>
					</table>
				</div>
				<div class="xinsu-page">
					<div class="xinsu-show">
						<{$page}>
					</div>
					<div class="xinsu-clear"></div>
				</div>
			</div>
		</div>
		<script>
			$(function(){
				get_stock();
			})
			var stock = 0;
			
			//获取最新价格
			function get_stock() {
				if (stock >= 0 && system_holiday == 0) {
					var url = "/stock/ajax_get_bull";
					var data = "";
					$(".xinsu-stock-status").each(function(){
						var tmp = $(this).attr("data");
						if (tmp != undefined) {
							data += "," + tmp;
						}
					})
					http_request_post(url, {data:data}, function(res) {
						var result = res.data;
						//列表
						$.each(result, function(key, val){
							$("#xinsu-stock-price-" + key).html(val.price);//当前价格
							$("#xinsu-stock-save-" + key).html(val.save);//留仓天数
							$("#xinsu-stock-gain-" + key).html(val.gain);//浮动盈亏
							$("#xinsu-stock-loss-" + key).html(val.loss);//盈亏比例

							if (val.gain > 0) {
								$("#xinsu-stock-gain-" + key).addClass("xinsu-fontcolor-red").removeClass("xinsu-fontcolor-green");
							} else {
								$("#xinsu-stock-gain-" + key).addClass("xinsu-fontcolor-green").removeClass("xinsu-fontcolor-red");
							}
							if (val.loss > 0) {
								$("#xinsu-stock-loss-" + key).addClass("xinsu-fontcolor-red").removeClass("xinsu-fontcolor-green");
							} else {
								$("#xinsu-stock-loss-" + key).addClass("xinsu-fontcolor-green").removeClass("xinsu-fontcolor-red");
							}
						})
					});
					
					http_request_post("/user/asset", {}, function(res){
						var result = res.data;
						//头部
						$(".xinsu-right-info").find('li').eq(0).html("资金余额：" + result.finance);
						$(".xinsu-right-info").find('li').eq(1).html("可取金额：" + result.cash);
						$(".xinsu-right-info").find('li').eq(2).html("可用金额：" + result.amount);
						$(".xinsu-right-info").find('li').eq(3).html("持仓总额：" + result.bull_money);
						$(".xinsu-right-info").find('li').eq(4).html("浮动盈亏合计：" + result.total_gain);
						$(".xinsu-right-info").find('li').eq(5).html("保证金率：" + result.ratio + "%");
					})
				}
				stock = setTimeout("get_stock();", 1000);
			}
			
			//刷新
			$(".xinsu-refresh").click(function(){
				location.reload();
			})

			//平仓
			$(".xinsu-block-sell").click(function(){
				var id = $(this).attr("data");
				var price = $("#xinsu-stock-price-" + id).html();
				var num = $(this).attr("data-num");
				var stock = $(this).attr("data-stock");
				var stock_name = $(this).attr("data-stockname");
				var can_sell_num = $(this).attr("data-cannum");
				var bull_price = $(this).attr("data-bullprice");
				var bull_type = $(this).attr("data-bulltype");
				var bull_time = $(this).attr("data-bulltime");
				if (bull_type == 1) {
					var title = "卖券还款";
					bull_type = '<td style="text-align: left;" class="xinsu-fontcolor-red">融资↑</td>';
				} else {
					var title = "买券还券";
					bull_type = '<td style="text-align: left;" class="xinsu-fontcolor-green">融券↓</td>';
				}
				
				//弹出卖出
				layer.open({ 
					type: 1,
					title: title,
					area: ['550px', '560px'],
					btn:['确定', '取消'],
					shadeClose: true, //点击遮罩关闭
					content: '<div style="padding:10px 40px 0 40px; line-height:36px; font-size:14px;"><form class="layui-form"><table class="layui-table"><tbody><tr><td>股票名称：</td><td style="text-align: left;">'+stock_name+'</td></tr><tr><td>股票代码：</td><td style="text-align: left;">'+stock+'</td></tr><tr><td>持仓时间：</td><td style="text-align: left;">'+bull_time+'</td></tr><tr><td>持仓数量：</td><td style="text-align: left;">'+num+'</td></tr><tr><td>股票类型：</td>'+bull_type+'</tr><tr><td>买入价格：</td><td style="text-align: left;" class="xinsu-fontcolor-green">'+bull_price+' 元/股 </td></tr><tr><td>当前价格：</td><td style="text-align: left;" class="xinsu-fontcolor-red">'+price+' 元/股</td></tr><tr><td>卖出价格</td><td style="text-align: left;"><input style="display:inline-block;" type="radio" name="price_type" value="0" checked onclick="javascript:$(\'#xinsu-sell-price\').hide();"><span class="xinsu-fontcolor-green"> 市价</span> &nbsp;<input style="display:inline-block;" type="radio" name="price_type" value="1" onclick="javascript:$(\'#xinsu-sell-price\').show();"><span class="xinsu-fontcolor-red"> 委托价</span></td></tr><tr id="xinsu-sell-price" style="display:none;"><td>委托价格：</td><td><input style="border:solid 1px #efefef; width:40%;" type="text" name="sell_price" id="sell_price" value="'+price+'" class="layui-input xinsu-fontcolor-red"/></td></tr><tr><td>卖出数量：</td><td><input style="border:solid 1px #efefef; width:40%;" type="text" name="sell_num" id="sell_num" class="layui-input" value="'+can_sell_num+'" placeholder="最大卖出数量'+can_sell_num+'股" /></td></tr></tbody></table></form></div>',
					yes:function(index, layers){
						stock = -1;
						var price_type = $("input[name='price_type']:checked").val();
						var sell_num = $("#sell_num").val();
						var sell_price = $("#sell_price").val();
						//交易类型
						if (price_type == 0) {
							var rmsg = "市价平仓处理成功！";
						} else {
							var rmsg = "限价平仓委托处理成功！";
						}
						http_request_post("/stock/ajax_set_sell", {id:id, sell_num:sell_num, sell_price:sell_price, price_type:price_type, token:token}, function(result){
							layer.closeAll();
							layer.alert(rmsg, {icon:1, closeBtn:0}, function(){
								location.reload();
							});
						});
					}
					
				});
			})
		</script>
<{include file="public/footer.html"}>